package com.jsgt.greekfire.dialog

import android.app.Dialog
import android.content.Context
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.jsgt.greekfire.R
import java.util.*
import kotlin.collections.ArrayList

/**
 * author fhx
 * create time 2020/5/4 9:24
 **/
class SecurityPwdDialog(context: Context, themeResId: Int) :
    Dialog(context, R.style.fullScreenDialog),
    View.OnClickListener {
    constructor(context: Context) : this(context, 0)

    var inputCallback: InputCallback? = null
    var numberTv: TextView? = null
    var nameTv: TextView? = null
    var iv_key_del: ImageView? = null
    private val pwdTvList = ArrayList<TextView>()//显示密码的6个
    private val inputTvList = ArrayList<TextView>()//可输入的10个
    var strBuilder = StringBuilder()
    init {
        val contentView = layoutInflater.inflate(R.layout.dialog_snap_up_input_pwd, null, false)
        numberTv = contentView.findViewById(R.id.buy_num_tv)
        nameTv = contentView.findViewById(R.id.coin_name_tv)

        iv_key_del = contentView.findViewById(R.id.iv_key_del)
        iv_key_del?.setColorFilter(ContextCompat.getColor(getContext(), R.color.address_text))

        val llTvPwdContainer = contentView.findViewById<LinearLayout>(R.id.ll_tv_pwd_container)
        for (i in 0 until llTvPwdContainer.childCount) {
            pwdTvList.add(llTvPwdContainer.getChildAt(i) as TextView)
        }
        val clKeyboradContainer =
            contentView.findViewById<ConstraintLayout>(R.id.cl_keyborad_container)
        val childCount = clKeyboradContainer.childCount
        for (i in 0 until childCount) {
            val view = clKeyboradContainer.getChildAt(i)
            view.setOnClickListener(this)
            if (view.isEnabled && view is TextView) {
                inputTvList.add(view)
            }
        }

        contentView.findViewById<ImageView>(R.id.dialog_close_iv).setOnClickListener {
            dismiss()
        }
        setContentView(contentView)
        setCancelable(true)
        setOnShowListener {
            clKeyboradContainer.postDelayed({
                clKeyboradContainer.visibility = View.VISIBLE
                val animation = AnimationUtils.loadAnimation(
                    context,
                    R.anim.bottom_in
                )
                clKeyboradContainer.startAnimation(animation)
            }, 300)
        }
    }

    override fun show() {
        strBuilder.setLength(0)//清空
        setPassword()
        randomText()
        super.show()
    }

    override fun onClick(v: View?) {
        if (v?.id == R.id.iv_key_del) {
            if (strBuilder.isNotEmpty()) {
                strBuilder.deleteCharAt(strBuilder.lastIndex)
                setPassword()
            }
        } else {
            //超过6个了不能再输入了
            if (strBuilder.length < 6) {
                strBuilder.append((v as TextView).text)
                setPassword()
                if (strBuilder.length == 6) {
                    dismiss()
                    inputCallback?.callBack(strBuilder.toString())
                }
            }
        }
    }

    private fun setPassword() {
        //先全部置空
        for (item in pwdTvList) {
            item.text = ""
        }

        for (index in strBuilder.indices) {
            pwdTvList[index].text = "*"
        }
    }

    interface InputCallback {
        fun callBack(result: String)
    }

    //随机设置键盘上的数字
    private fun randomText() {
//        val list = ArrayList<Int>()
//        val ran = Random()
//        while (list.size < 10) {
//            val n = ran.nextInt(10);
//            if (n !in list)
//                list.add(n)//如果n不包涵在list中，才添加
//        }
//
//        for (i in list.indices) {
//            inputTvList[i].text = list[i].toString()
//        }

        val set: MutableSet<Int> = LinkedHashSet()
        while (set.size < 10) {
            val num = (Math.random() * 10).toInt()
            set.add(num)
        }
        val it: Iterator<*> = set.iterator()
        var index = 0;
        while (it.hasNext()){
            inputTvList[index].text = it.next().toString()
            index++
        }
    }
}